package com.example.demo.controller;

import com.example.demo.service.FileService;
import com.example.demo.service.LabService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/labs/{labId}/images")
@RequiredArgsConstructor
public class LabImageController {

    private final LabService labService;
    private final FileService fileService;

    @PostMapping
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<?> uploadImage(
            @PathVariable Long labId,
            @RequestParam("file") MultipartFile file) {
        String imageUrl = fileService.uploadLabImage(file);
        return ResponseEntity.ok(labService.addImage(labId, imageUrl));
    }

    @DeleteMapping("/{imageUrl}")
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<?> deleteImage(
            @PathVariable Long labId,
            @PathVariable String imageUrl) {
        fileService.deleteFile(imageUrl);
        return ResponseEntity.ok(labService.removeImage(labId, imageUrl));
    }
} 